package com.atguigu.lease.common.test;

import io.jsonwebtoken.*;
import io.jsonwebtoken.security.Keys;

import javax.crypto.SecretKey;

public class T2 {
    // 服务器service_key
    static String service_Key = "54458987954665645454546546615516541";
    public static void main(String[] args) {
        String token = "eyJ0eXBlIjoiSldUIiwiYWxnIjoiSFMyNTYifQ.eyJpZCI6MSwibmFtZSI6IlRvbWkifQ.54J2S1kojlDPsi5TWlZey5m3VGkEAvsJuLJZPZTxmaE";
        //环境
        String ip = "127.0.0.2";
        // 解析
        String salt = ip + service_Key;
        SecretKey secretKey = Keys.hmacShaKeyFor(salt.getBytes());

        JwtParser parser = Jwts.parserBuilder().setSigningKey(secretKey).build();

        Jws<Claims> claimsJws = parser.parseClaimsJws(token);
        System.out.println(claimsJws);
    }

    public static String createToken(){


        //环境
        String ip = "127.0.0.1";

        //用户信息
        User user = new User();
        user.setName("Tomi");
        user.setAge(18);
        user.setId(1l);

        // 签名盐
        String salt = ip + service_Key;
        SecretKey secretKey = Keys.hmacShaKeyFor(salt.getBytes());

        String token = Jwts.builder()
                .setHeaderParam("type", "JWT")
                .setHeaderParam("alg","HS256")
                .claim("id", user.getId())
                .claim("name", user.getName())
                .signWith(secretKey)
                .compact();
        System.out.println(token);
        return  token;
    }




}
